$.fn.pagination = function () {
  var options = $(this).data();
  if(arguments.length != 1) {
    options.page = arguments[0];
    options.allPage = arguments[1];
    options.success = arguments[2];
  } else options = arguments[0];
  var defaults = {
    page : 1,
    allPage : 10,
    isFL : false,
    firstTxt : '&le;',
    firstClass : 'pagination-first',
    lastTxt : '&ge;',
    lastClass : 'pagination-last',
    isPN : true,
    prevTxt : '&laquo;',
    prevClass : 'pagination-prev',
    nextTxt : '&raquo;',
    nextClass : 'pagination-next',
    btnNum : 5,
    btnClass : 'pagination-num',
    activeClass : 'active',
    disableClass : 'disabled',
    success : function () {},
    init : function () {},
    createBtn : function (className, string) {
      return '<li class="' + className + '"><a href="javascript:;">' + string + '</a></li>';
    }
  },
  html = '',
  _this = this,
  $this = $(this),
  o = $.extend({}, defaults, options);
  
  if(o.page == 0 || o.allPage == 0) return;

  this.createBtn = function (className, string) {
    html += o.createBtn(className, string);
  };

  if(o.isFL) _this.createBtn(o.firstClass + ' ' + (o.page == 1 ? o.disableClass : ''), o.firstTxt);
  if(o.isPN) _this.createBtn(o.prevClass + ' ' + (o.page == 1 ? o.disableClass : ''), o.prevTxt);

  if (o.page <= Math.ceil(o.btnNum / 2)) for (var i = 1; i <= o.allPage && i <= o.btnNum; i++) _this.createBtn((o.page == i ? (o.activeClass + ' ' + o.disableClass) : o.btnClass), i);
  else if (o.page > Math.ceil(o.btnNum / 2) && o.page < o.allPage - Math.floor(o.btnNum / 2)) for (var i = 1, j = o.page - Math.floor(o.btnNum / 2); i <= o.btnNum; i++, j++) _this.createBtn((o.page == j ? (o.activeClass + ' ' + o.disableClass) : o.btnClass), j);
  else for (var i = 1; i <= o.btnNum; i++) _this.createBtn((o.page == o.allPage - o.btnNum + i ? (o.activeClass + ' ' + o.disableClass) : o.btnClass), (o.allPage - o.btnNum + i));
  
  if(o.isPN) _this.createBtn(o.nextClass + ' ' + (o.page == o.allPage ? o.disableClass : ''), o.nextTxt);
  if(o.isFL) _this.createBtn(o.lastClass + ' ' + (o.page == o.allPage ? o.disableClass : ''), o.lastTxt);

  $this.html(html);

  this.judge = function (className, number) {
    $this.children('.' + className).click(function () {
      if(!$(this).hasClass(o.disableClass)) o.success(number);
    });
  };

  _this.judge(o.firstClass, 1);
  _this.judge(o.lastClass, o.allPage);
  _this.judge(o.prevClass, o.page - 1);
  _this.judge(o.nextClass, o.page + 1);
  
  $this.children('.' + o.btnClass).click(function () {
    o.success($(this).text());
  });

  o.init();
};